import {Component,OnInit, EventEmitter, Output} from "@angular/core";
import {Router} from "@angular/router";
import {NzModalService, NzModalSubject, NzNotificationService} from "ng-zorro-antd";
import {HttpServcie} from "../../../service/http.service";
import {TrainingPlanBean} from "../bean/trainingPlan.bean";
import {FormBuilder,FormGroup,Validators} from "@angular/forms";
import * as moment from "moment";
import {Config} from "../../../service/config";


@Component({
  selector: "trainingPlan-add-page",
  templateUrl: "trainingPlan-add.html",
  styleUrls: ["trainingPlan-add.scss"]
})

export class TrainingPlanAdd implements OnInit{
  constructor(
    private httpService: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private fb: FormBuilder
  ) { }
  @Output() outValue = new EventEmitter();
  validateForm: FormGroup;
  tpBean : TrainingPlanBean = new TrainingPlanBean();
  config = Config;

  ngOnInit(){
    this.validateForm = this.fb.group({
      planName: [null, [Validators.required]], //计划名称
      planType: [null, [Validators.required]], //计划类型
      planContent: [null, [Validators.required]], //计划描述
      leadUnitName: [null, [Validators.required]], //牵头单位
      userName: [null, [Validators.required]], //负责人
      startDate: [null, [Validators.required]], //开始时间
      endDate: [null, [Validators.required]] //结束时间
    });

    this.tpBean.leadUnitId = JSON.parse(window.localStorage.getItem("user")).partyId;
    this.tpBean.leadUnitName = JSON.parse(window.localStorage.getItem("user")).partyName;
    this.tpBean.partyLevel = JSON.parse(window.localStorage.getItem("user")).partyLevel;
  }

  submitForm(){
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }

    for (const i in this.validateForm.controls) {
      if (!this.validateForm.controls[i].valid) {
        return;
      }
    }
    /*判断字数个数*/
    if (this.tpBean.planContent.length > 150) {
      this._notification.create("error", "错误提示", "输入的数字个数不能大于150个！");
      return;
    }

    if(this.tpBean.endDate <= this.tpBean.startDate ){
      this._notification.create("warning", "警告提示", "结束时间不能小于或等于开始时间！");
      return false;
    }

    this.tpBean.startDate = moment(this.tpBean.startDate).format("YYYYMMDD");
    this.tpBean.endDate = moment(this.tpBean.endDate).format("YYYYMMDD");

    this.httpService.addTrainingPlan(this.tpBean).subscribe(data => {
      if (data["status"] != "200") {
        this._notification.create("error", "错误提示", "新增失败！");
        return;
      }
      this.outValue.emit(true);
    });
  }

  quit() {
    this.outValue.emit(true);
  }

  getFormControl(name) {
    return this.validateForm.controls[name];
  }
  // 查询面板
  panels = {
    active: true,
    name: "查询",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "查询"
      }
    ]
  };

}
